package com.ctsi.ssdc.web;

import java.net.URI;
import java.net.URISyntaxException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Optional;
import java.lang.Integer;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.util.Assert;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import com.ctsi.ssdc.service.LitemallCommentService;
import com.ctsi.ssdc.domain.LitemallComment;

import com.ctsi.ssdc.exception.BadRequestAlertException;
import com.ctsi.ssdc.model.PageResult;
import com.ctsi.ssdc.util.HeaderUtil;
import com.ctsi.ssdc.util.ResponseUtil;


/**
 * REST controller for managing LitemallComment.
 *
 * @author ctsi-biyi-generator
 *
 */
@Api(value = "/api",tags = {"litemall-comment-controller"})
@RestController
@RequestMapping("/api")
public class LitemallCommentController {

    private final Logger log = LoggerFactory.getLogger(LitemallCommentController.class);

    private static final String ENTITY_NAME = "litemallComment";

    private final LitemallCommentService litemallCommentService;

    public LitemallCommentController(LitemallCommentService litemallCommentService) {
        this.litemallCommentService = litemallCommentService;
    }

    @InitBinder   
    public void initBinder(WebDataBinder binder) {   
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");   
        dateFormat.setLenient(true);   
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));   
    } 
   
    /**
     * POST  /litemallComments : Create a new litemallComment.
     *
     * @param litemallComment the litemallComment to create
     * @return the ResponseEntity with status 201 (Created) and with body the new litemallComment,
        or with status 400 (Bad Request) if the litemallComment has already an id
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @ApiImplicitParams({
        @ApiImplicitParam(paramType = "body",dataType = "LitemallComment",name = "litemallComment",value = "the litemallComment to create")
    })
    @ApiOperation(value = "POST  /litemallComments : create a new litemallComment.",
        notes = "POST  /litemallComments : create a new litemallComment.",httpMethod = "POST")
    @PostMapping("/litemallComments")
    public ResponseEntity<LitemallComment> createLitemallComment(@RequestBody LitemallComment litemallComment) throws URISyntaxException {
        log.debug("REST request to save LitemallComment : {}", litemallComment);
        if (litemallComment.getId() != null) {
            throw new BadRequestAlertException("A new litemallComment cannot already have an id", ENTITY_NAME, "idexists");
        }

        boolean save =  litemallCommentService.save(litemallComment);
        Assert.isTrue(save,"Insert failed");
        LitemallComment result=litemallComment;

        return ResponseEntity.created(new URI("/api/litemallComments" + "/" +result.getId() ))
            .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString()))
            .body(result);
    }
	
    /**
     * PUT  /litemallComments : Updates an existing litemallComment.
     *
     * @param litemallComment the litemallComment to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated litemallComment,
     * or with status 400 (Bad Request) if the litemallComment is not valid,
     * or with status 500 (Internal Server Error) if the litemallComment couldn't be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @ApiImplicitParams({
        @ApiImplicitParam(paramType = "body",dataType = "LitemallComment",name = "litemallComment",value = "the litemallComment to update")
    })
    @ApiOperation(value = "PUT  /litemallComments : updates an existing litemallComment.",
        notes = "PUT  /litemallComments : updates an existing litemallComment.",httpMethod = "PUT")
    @PutMapping("/litemallComments")
    public ResponseEntity<LitemallComment> updateLitemallComment(@RequestBody LitemallComment litemallComment) throws URISyntaxException {
        log.debug("REST request to update LitemallComment : {}", litemallComment);
	    if (litemallComment.getId() == null) {
	    	return createLitemallComment(litemallComment);
	    }

        boolean update = litemallCommentService.updateById(litemallComment);
        Assert.isTrue(update,"Update failed");
        LitemallComment result=litemallComment;

        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, result.getId().toString()))
            .body(result);
    }

    /**
     * GET  /litemallComments/:id : get the "id" litemallComment.
     *
     * @param id the id of the litemallComment to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the litemallComment, or with status 404 (Not Found)
     */
    @ApiImplicitParams({
        @ApiImplicitParam(paramType = "path",dataType = "INTEGER",
        name = "id",value = "the id of the litemallComment to retrieve")
    })
    @ApiOperation(value = "GET  /litemallComments/id : get the id litemallComment.",
        notes = "GET  /litemallComments/id : get the id litemallComment.",httpMethod = "GET")
    @GetMapping("/litemallComments/{id}")
    public ResponseEntity<LitemallComment> getLitemallComment(@PathVariable Integer id) {
        log.debug("REST request to get LitemallComment : {}", id);
        LitemallComment litemallComment = litemallCommentService.getById(id);
        return ResponseUtil.wrapOrNotFound(Optional.ofNullable(litemallComment));
    }

    /**
    * GET  /litemallComments : get the litemallComments firstStringBaseColumn.
    *
    * @return the ResponseEntity with status 200 (OK) and the list of litemallComments in body
    */
    @ApiOperation(value = "GET  /litemallComments/id : get the id litemallComment.",
        notes = "GET  /litemallComments/id : get the id litemallComment.",httpMethod = "GET")
    @GetMapping("/litemallComments")
    public PageResult<LitemallComment> getLitemallCommentsList(String content ,Pageable pageable) {
        log.debug("REST request to get LitemallCommentsList");
        return litemallCommentService.findFirstStringColumn(content ,pageable);
    }
	
    /**
     * DELETE  /litemallComments/:id : delete the "id" litemallComment.
     *
     * @param id the id of the litemallComment to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @ApiImplicitParams({
        @ApiImplicitParam(paramType = "path",dataType = "INTEGER",name = "id",
        value = "the id of the litemallComment to delete")
    })
    @ApiOperation(value = "DELETE  /litemallComments/id : delete the id litemallComment.",
        notes = "DELETE  /litemallComments/id : delete the id litemallComment.",httpMethod = "DELETE")
    @DeleteMapping("/litemallComments/{id}")
    public ResponseEntity<Void> deleteLitemallComment(@PathVariable Integer id) {
        log.debug("REST request to delete LitemallComment : {}", id);
        litemallCommentService.removeById(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }
    
}
